//
//  CTAnnouncementVC.m
//  digitalCurrency
//
//  Created by 默认账户 on 2019/6/3.
//  Copyright © 2019 XinHuoKeJi. All rights reserved.
//

#import "CTAnnouncementVC.h"
#import "CTAnnouncementCell.h"
#import "PlatformMessageModel.h"
#import "HomeNetManager.h"

@interface CTAnnouncementVC ()
@property (nonatomic, strong) NSMutableArray *platformMessageArr;

@property (nonatomic, strong) LYEmptyView *emptyView;

@end

@implementation CTAnnouncementVC
- (instancetype)init
    {
        self = [super init];
        if (self) {
            _platformMessageArr = [NSMutableArray array];
        }
        return self;
    }
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationController setNavigationBarHidden:NO animated:YES];

    self.viewTitle.text = LocalizationKey(@"Details of announcement");
    [self.view addSubview:self.tableView];
    self.tableView.rowHeight = 148;
    self.tableView.separatorColor = [UIColor clearColor];
    [self.tableView registerClass:[CTAnnouncementCell class] forCellReuseIdentifier:@"announcecell"];

    [self headRefreshWithScrollerView:self.tableView];
    [self getNotice];
    
}

#pragma mark-下拉刷新数据
- (void)refreshHeaderAction {
    [self getNotice];
}

//- (void)viewWillDisappear:(BOOL)animated {
//    [super viewWillDisappear:animated];
//    [self.navigationController setNavigationBarHidden:YES animated:YES];
//}
//
//- (void)viewDidAppear:(BOOL)animated {
//    [super viewDidAppear:animated];
//    [self.navigationController setNavigationBarHidden:NO animated:YES];
//}
    
    
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _platformMessageArr.count;
}
    
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CTAnnouncementCell *cell = [CTAnnouncementCell createTableView:tableView];
    PlatformMessageModel *model = _platformMessageArr[indexPath.row];
    cell.titLabel.text   = model.name;
    cell.contentLabel.text = model.content;
    cell.dateLabel.text    = model.createTime;
    
    return cell;
}
    
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    PlatformMessageModel *model = _platformMessageArr[indexPath.row];
    return [self heightWithText:model.content];
}
    /** 计算cell高度 */
- (CGFloat)heightWithText:(NSString *)text {
    NSString *string = text;
    
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:string];
    NSRange allRange = [string rangeOfString:string];
    [attrStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12.0] range:allRange];
    [attrStr addAttribute:NSForegroundColorAttributeName value:[UIColor darkGrayColor]range:allRange];
    
    CGFloat titleHeight;
    NSStringDrawingOptions options =  NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading;
    // 获取label的最大宽度
    CGRect rect = [attrStr boundingRectWithSize:CGSizeMake(SCREEN_WIDTH-116, CGFLOAT_MAX)options:options context:nil];
    titleHeight = ceilf(rect.size.height);
    
    return titleHeight + 130; // 动态高度 + 静态高度
}
    
#pragma mark-获取平台消息
-(void)getNotice {
    [EasyShowLodingView showLodingText:LocalizationKey(@"loading")];
    [HomeNetManager getPlatformMessageForCompleteHandleWithPageNo:@"1" withPageSize:@"20" CompleteHandle:^(id resPonseObj, int code) {
        [EasyShowLodingView hidenLoding];
        //NSLog(@"获取平台消息 ---- %@", resPonseObj);
        
        if (code) {
            if ([resPonseObj[@"code"] integerValue] == 0) {
                [self.platformMessageArr removeAllObjects];
                NSArray *arr = resPonseObj[@"data"][@"list"];
                NSMutableArray *muArr = [NSMutableArray arrayWithCapacity:0];
                for (NSDictionary *dic in arr) {
                    [muArr addObject:dic];
//                    if ([[ChangeLanguage userLanguage] isEqualToString:@"en"]) {
//                        if (![self hasChinese:dic[@"title"]]) {
//                            [muArr addObject:dic];
//                        }
//                    }
//                    else {
//                        if ([self hasChinese:dic[@"title"]]) {
//                            [muArr addObject:dic];
//                        }
//                    }
                }
                NSArray *dataArr = [PlatformMessageModel mj_objectArrayWithKeyValuesArray:muArr];
                [_platformMessageArr addObjectsFromArray:dataArr];
                
                if (self.platformMessageArr.count == 0) {
                    self.tableView.ly_emptyView = self.emptyView;
                }
                
            }else{
                [self.view makeToast:resPonseObj[MESSAGE] duration:1.5 position:CSToastPositionCenter];
            }
            
            [self.tableView reloadData];
        }else{
            [self.view makeToast:LocalizationKey(@"noNetworkStatus") duration:1.5 position:CSToastPositionCenter];
        }
    }];
}
- (BOOL)hasChinese:(NSString *)str {
    for(int i=0; i< [str length];i++){
        int a = [str characterAtIndex:i];
        if( a > 0x4e00 && a < 0x9fff)
        {
            return YES;
        }
    }
    return NO;
}

- (LYEmptyView *)emptyView {
    if (!_emptyView) {
        _emptyView = [LYEmptyView emptyViewWithImageStr:@"emptyData" titleStr:[[ChangeLanguage bundle] localizedStringForKey:@"NoData" value:nil table:@"English"]];
        _emptyView.titleLabTextColor = tThreeColor;
        _emptyView.titleLabFont = [UIFont systemFontOfSize:19];
        _emptyView.contentViewY = SCREEN_HEIGHT/2.0-100;
    }
    return _emptyView;
}

    
@end
